﻿using System;
using System.Collections;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using BHXH.Repository;
namespace Lists
{
    public class Phieu_DangBang : DoiTuong
    {
        #region Phieu property
        public Phieu Phieu
        {
            get
            {
                if (_DataRow["PhieuID"] is Guid)
                    return (Phieu)DanhMucPhieu.Instance.GetByID((Guid)_DataRow["PhieuID"]);
                else return null;
            }
            set
            {
                if (value is Phieu)
                    _DataRow["PhieuID"] = value.ID;
                else
                    _DataRow["PhieuID"] = DBNull.Value;
            }
        }
        #endregion

        #region MaCau property
        public int MaCau
        {
            get
            {
                if (_DataRow["MaCau"] is int) return (int)_DataRow["MaCau"];
                else return -1;
            }
            set
            {
                _DataRow["MaCau"] = value;
            }
        }
        #endregion

        #region Number1 property
        public int Number1
        {
            get
            {
                if (_DataRow["Number1"] is int) return (int)_DataRow["Number1"];
                else return -1;
            }
            set
            {
                _DataRow["Number1"] = value;
            }
        }
        #endregion

        #region Number2 property
        public int Number2
        {
            get
            {
                if (_DataRow["Number2"] is int) return (int)_DataRow["Number2"];
                else return -1;
            }
            set
            {
                _DataRow["Number2"] = value;
            }
        }
        #endregion

        #region Number3 property
        public int Number3
        {
            get
            {
                if (_DataRow["Number3"] is int) return (int)_DataRow["Number3"];
                else return -1;
            }
            set
            {
                _DataRow["Number3"] = value;
            }
        }
        #endregion

        #region Number4 property
        public int Number4
        {
            get
            {
                if (_DataRow["Number4"] is int) return (int)_DataRow["Number4"];
                else return -1;
            }
            set
            {
                _DataRow["Number4"] = value;
            }
        }
        #endregion

        public Phieu_DangBang(DataRow dataRow)
            : base(dataRow)
        {
            //this.TrangThai = true;
        }
    }

    public class DanhMucPhieu_DangBang : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucPhieu_DangBang _Instance;
        public static DanhMucPhieu_DangBang Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucPhieu_DangBang();
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                }
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "Phieu_DangBang";
            }
        }
        #endregion

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new Phieu_DangBang((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public Phieu_DangBang this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new Phieu_DangBang(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }
        public Phieu_DangBang this[int indexView, bool a]
        {
            get
            {
                if (0 <= indexView && indexView <= this.Count)
                {
                    return new Phieu_DangBang(DataView.ToTable().Rows[indexView]);
                }
                else
                {
                    return null;
                }
            }
        }

        public Phieu_DangBang NewDoiTuong()
        {
            return new Phieu_DangBang(DataTable.NewRow());
        }

        public Phieu_DangBang GetByProperty(string property, object value)
        {
            try
            {
                DataRow[] rows = GetDataRows(property, value);
                if (rows.Length > 0) return new Phieu_DangBang(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public Phieu_DangBang GetByID(Guid objID)
        {
            return GetByProperty("ID", objID);
        }

        public static DanhMucPhieu_DangBang GetAll()
        {
            return Instance;
        }

        public static DanhMucPhieu_DangBang Filter(string filterExpression)
        {
            try
            {
                DanhMucPhieu_DangBang obj = new DanhMucPhieu_DangBang();
                obj.DataView = new DataView(DanhMucPhieu_DangBang.Instance.DataTable, filterExpression, "PhieuID", DataViewRowState.CurrentRows);
                return obj;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static Phieu_DangBang GetByPhieuID_MaCau(Guid phieuID, int maCau)
        {
            try
            {
                DanhMucPhieu_DangBang dm = Filter(string.Format("PhieuID = '{0}' AND MaCau = {1}", phieuID, maCau));
                if (dm.Count >= 1) return dm[0, true];
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static bool CheckHaveByPhieuID_MaCau(Guid phieuID, int maCau)
        {
            try
            {
                DanhMucPhieu_DangBang dm = Filter(string.Format("PhieuID = '{0}' AND MaCau = {1}", phieuID, maCau));
                if (dm.Count >= 1) return true;
                else return false;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
        public static DanhMucPhieu_DangBang GetByPhieuID(Guid phieuID)
        {
            try
            {
                DanhMucPhieu_DangBang dm = Filter(string.Format("PhieuID = '{0}' ", phieuID));
                if (dm.Count >= 1) return dm;
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
    }
}
